package net.ubinow.mflex.data;

import net.ubinow.mflex.util.Constants;



public class SimpleDataItem implements IDataItem {
    
    private String _assetID = Constants.UNDEFINED;
    private StringBuffer _value = null; 
    private boolean _flag = false; 
        
    public SimpleDataItem() {
        _value = new StringBuffer(Constants.UNDEFINED);
    }
    
    public SimpleDataItem( String value ){
        _value = new StringBuffer( value );
    }    
    
    public SimpleDataItem( String assetID, String value ){
        _assetID = assetID; 
        _value = new StringBuffer( value );
    }

    public String getAssetID() {
        return _assetID; 
    }

    public String getText() {
        return _value.toString(); 
    }

    public void setText(String val) {
        if( _value.length() > 0 ){
            _value.delete( 0, _value.length()-1 );
            if( _value.length() == 1 ){
                _value.deleteCharAt( 0 );
            }
        }
        
        _value.append( val );
    }
    
    public void appendText( String text ){
        _value.append( text );        
    }
    
    public void appendText( char c ){
        _value.append( c );        
    }
    
    public void insertText( int index, String text ){
        _value.insert( index, text );
    }
    
    public void insertText( int index, char c ){
        _value.insert( index, c );
    }
    
    public StringBuffer getRawDataItem(){
        return _value; 
    }

    public String getText( String fields ) {
        return _value.toString(); 
    }

    public int compareTo(Object o) {
        if( !(o instanceof SimpleDataItem) )
            return -1; 
        
        SimpleDataItem i = (SimpleDataItem)o; 
        
        if( !_assetID.equalsIgnoreCase( Constants.UNDEFINED ) ){
            return _assetID.compareTo( i.getAssetID() );
        }
        else{
            return _value.toString().compareTo( i.getText() ); 
        }
    }
    
    public boolean equals( Object o ){
        if( !(o instanceof SimpleDataItem) )
            return false; 
        
        SimpleDataItem i = (SimpleDataItem)o; 
        
        if( !_assetID.equalsIgnoreCase( Constants.UNDEFINED ) ){
            return _assetID.equals( i.getAssetID() );
        }
        else{
            return _value.equals( i.getText() ); 
        }
    }
    
    public boolean getFlag(){
        return _flag;
    }
    
    public void setFlag( boolean flag ){
        _flag = flag;
    }
    
}

